package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StreamProxyMapper {

    @Insert(value = {"<script> INSERT INTO stream_proxy (type, app, stream, url, src_url, dst_url, " +
            "timeout_ms, ffmpeg_cmd_key, rtp_type, enable_hls, enable_mp4, enable) VALUES" +
            "('${type}','${app}', '${stream}', '${url}', '${src_url}', '${dst_url}', " +
            "'${timeout_ms}', '${ffmpeg_cmd_key}', '${rtp_type}', "+
            "<if test=\"enable_hls==true\"> 1 ,</if> "+
            "<if test=\"enable_hls!=true\"> 0 ,</if> "+
            "<if test=\"enable==true\"> 1 ,</if> "+
            "<if test=\"enable!=true\"> 0 ,</if> "+
            "<if test=\"enable_mp4==true\"> 1 ,</if> "+
            "<if test=\"enable_mp4!=true\"> 0 ,</if> "+
    		") </script>"})
    int add(StreamProxyItem streamProxyDto);

    @Update(value = {"<script> UPDATE stream_proxy " +
            "SET type=#{type}, " +
            "app=#{app}," +
            "stream=#{stream}," +
            "url=#{url}, " +
            "src_url=#{src_url}," +
            "dst_url=#{dst_url}, " +
            "timeout_ms=#{timeout_ms}, " +
            "ffmpeg_cmd_key=#{ffmpeg_cmd_key}, " +
            "rtp_type=#{rtp_type}, " +
            "<if test=\"enable_hls != null and enable_hls==true\"> enable_hls=1 ,</if> "+
            "<if test=\"enable_hls != null and enable_hls!=true\"> enable_hls=0 ,</if> "+
            "<if test=\"enable != null and enable==true\"> enable=1 ,</if> "+
            "<if test=\"enable != null and enable!=true\"> enable=0 ,</if> "+
            "<if test=\"enable_mp4 != null and enable_mp4==true\"> enable_mp4=1 ,</if> "+
            "<if test=\"enable_mp4 != null and enable_mp4!=true\"> enable_mp4=0 ,</if> "+
            "WHERE app=#{app} AND stream=#{stream}"+
    		" </script>"})
    int update(StreamProxyItem streamProxyDto);

    @Delete("DELETE FROM stream_proxy WHERE app=#{app} AND stream=#{stream}")
    int del(String app, String stream);

    @Select("SELECT st.*, pgs.gb_id, pgs.name, pgs.longitude, pgs.latitude FROM stream_proxy st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream")
    @ResultMap("streamProxy")
    List<StreamProxyItem> selectAll();

    @Select(value = {"<script> SELECT st.*, pgs.gb_id, pgs.name, pgs.longitude, pgs.latitude FROM stream_proxy st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream WHERE <if test=\"enable == true\">st.enable=1</if><if test=\"enable!=true\">st.enable=0</if> </script>"})
    @ResultMap("streamProxy")
    List<StreamProxyItem> selectForEnable(boolean enable);

    @Select("SELECT st.*, pgs.gb_id, pgs.name, pgs.longitude, pgs.latitude FROM stream_proxy st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream WHERE st.app=#{app} AND st.stream=#{stream}")
    @Results(id="streamProxy",
    value = { 
       @Result(property = "type", column = "type"), @Result(property = "app", column = "app"), 
       @Result(property = "stream", column = "stream"), @Result(property = "url", column = "url"), 
       @Result(property = "src_url", column = "src_url"), @Result(property = "src_url", column = "src_url"), 
       @Result(property = "timeout_ms", column = "timeout_ms"), @Result(property = "ffmpeg_cmd_key", column = "ffmpeg_cmd_key"), 
       @Result(property = "rtp_type", column = "rtp_type"), @Result(property = "enable", column = "enable"), 
       @Result(property = "enable_hls", column = "enable_hls"), @Result(property = "enable_mp4", column = "enable_mp4"), 
       @Result(property = "gbId", column = "gb_id"),
       @Result(property = "name", column = "name"),
       @Result(property = "longitude", column = "longitude"),
       @Result(property = "latitude", column = "latitude"),
       })
    StreamProxyItem selectOne(String app, String stream);
}
